package com.gensdai.leliang.common.adapter;

import android.content.Context;
import android.net.Uri;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.alibaba.android.vlayout.DelegateAdapter;
import com.alibaba.android.vlayout.LayoutHelper;
import com.alibaba.android.vlayout.VirtualLayoutManager;
import com.facebook.drawee.view.SimpleDraweeView;
import com.gensdai.leliang.R;
import com.gensdai.leliang.common.bean.CommentInfoBean;
import com.gensdai.leliang.view.ColoredRatingBar;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 首页功能按钮的适配
 * Created by LT on 2017/6/15.
 */

public class CommentAdapterRec extends DelegateAdapter.Adapter<CommentAdapterRec.MainViewHolder> {

    private Context mContext;

    private LayoutHelper mLayoutHelper;


    private VirtualLayoutManager.LayoutParams mLayoutParams;
    List<CommentInfoBean> blist;

    public CommentAdapterRec(Context context, LayoutHelper layoutHelper, List<CommentInfoBean> list) {
        this(context, layoutHelper, list, new VirtualLayoutManager.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
    }

    public CommentAdapterRec(Context context, LayoutHelper layoutHelper, List<CommentInfoBean> list, @NonNull VirtualLayoutManager.LayoutParams layoutParams) {
        this.mContext = context;
        this.mLayoutHelper = layoutHelper;
        this.blist = list;
        this.mLayoutParams = layoutParams;
    }

    @Override
    public LayoutHelper onCreateLayoutHelper() {
        return mLayoutHelper;
    }

    @Override
    public MainViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new MainViewHolder(LayoutInflater.from(mContext).inflate(R.layout.item_comment, parent, false));
    }

    @Override
    public void onBindViewHolder(MainViewHolder holder, int position) {
        holder.itemView.setLayoutParams(
                new VirtualLayoutManager.LayoutParams(mLayoutParams));

        CommentInfoBean info = blist.get(position);
        holder.commentName.setText(info.nickname);
        holder.commentRating.setRating(Float.valueOf(info.avgScore));
        holder.commentContent.setText(info.commentInfo.trim());
        holder.commentAttribute.setText(info.attributeName);
        holder.commentTime.setText(info.createTime);

        if (TextUtils.isEmpty(info.handpic)) {
            Uri uri = Uri.parse("");
            holder.commentIcon.setImageURI(uri);
        } else {
            Uri uri = Uri.parse(info.handpic);
            holder.commentIcon.setImageURI(uri);
        }

    }

    @Override
    public int getItemCount() {
        return blist.size();
    }


    static class MainViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.comment_icon)
        SimpleDraweeView commentIcon;
        @BindView(R.id.commentName)
        TextView commentName;
        @BindView(R.id.commentRating)
        ColoredRatingBar commentRating;
        @BindView(R.id.comment_content)
        TextView commentContent;
        @BindView(R.id.comment_attribute)
        TextView commentAttribute;
        @BindView(R.id.commentTime)
        TextView commentTime;

        public MainViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }

        @Override
        protected void finalize() throws Throwable {
            super.finalize();
        }
    }

}
